package com.rigi.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rigi.common.BaseContext;
import com.rigi.common.R;
import com.rigi.entity.ShoppingCart;
import com.rigi.entity.User;
import com.rigi.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 根据UserId获取购物车列表
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        Long currentId = BaseContext.getCurrentId();
        QueryWrapper<ShoppingCart> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",currentId);
        wrapper.orderByAsc("create_time");
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     * 添加购物车
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){

        Long currentId = BaseContext.getCurrentId();


        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();

        shoppingCartQueryWrapper.eq("user_id",currentId);

        if (shoppingCart.getDishId()!=null){
            shoppingCartQueryWrapper.eq("dish_id",shoppingCart.getDishId());
        }
        if (shoppingCart.getSetmealId()!=null){
            shoppingCartQueryWrapper.eq("setmeal_id",shoppingCart.getSetmealId());
        }
        ShoppingCart cart = shoppingCartService.getOne(shoppingCartQueryWrapper);
        if (cart!=null){
            Integer number = cart.getNumber();
            cart.setNumber(number+1);
            shoppingCartService.updateById(cart);
            return R.success(cart);
        }else {
            shoppingCart.setUserId(currentId);
            shoppingCart.setCreateTime(new Date());
            shoppingCartService.save(shoppingCart);
            return R.success(shoppingCart);
        }
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        Long currentId = BaseContext.getCurrentId();

        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("user_id",currentId);
        shoppingCartService.remove(shoppingCartQueryWrapper);

        return R.success("清空购物车成功");
    }

    /**
     * 菜品/套餐数量减一，若数量已经是一，则删除
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        Long currentId = BaseContext.getCurrentId();

        QueryWrapper<ShoppingCart> shoppingCartQueryWrapper = new QueryWrapper<>();
        shoppingCartQueryWrapper.eq("user_id",currentId);

        if (shoppingCart.getDishId()!=null){
            shoppingCartQueryWrapper.eq("dish_id",shoppingCart.getDishId());
        }
        if (shoppingCart.getSetmealId()!=null){
            shoppingCartQueryWrapper.eq("setmeal_id",shoppingCart.getSetmealId());
        }

        ShoppingCart cart = shoppingCartService.getOne(shoppingCartQueryWrapper);

        if (cart.getNumber()==1){
            shoppingCartService.removeById(cart);
            return R.success(cart);
        }else {
            Integer number = cart.getNumber();
            cart.setNumber(number-1);
            shoppingCartService.updateById(cart);
            return R.success(shoppingCart);
        }
    }
}
